package cn.tiga.user.utli;

import cn.tiga.basic.jwt.JwtUtils;
import cn.tiga.basic.jwt.Payload;
import cn.tiga.basic.jwt.RsaUtils;
import cn.tiga.user.domain.LoginData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.security.PublicKey;

/**
 * 获取登录信息的上下文：把登录的相关信息放到这里面，方便调用
 */
@Slf4j
public class LoginContext {
    /**
     * @param request
     * @return 如果是后台需要返回employee，如果是前台返回user
     */
    public static Object getLoginUser(HttpServletRequest request){
        try {
            String token = request.getHeader("token");
            if (StringUtils.isEmpty(token)){
                return null;
            }
            //获取公钥
            PublicKey pubKey = RsaUtils.getPublicKey(JwtUtils.class.getClassLoader().getResource("auth_rsa.pub").getFile());
            //获取载荷 = 数据
            Payload<LoginData> payload = JwtUtils.getInfoFromToken(token, pubKey, LoginData.class);

            LoginData loginData = payload.getLoginData();
            return loginData.getLogininfo();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}